package edu.fsu.cs.SHPE;

import java.net.ConnectException;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;


public class VenuMapActivity extends MapActivity {

	private final static String TAG = "SHPE.VenueMapActivity";
	private MapView venueMap;
	private VenueContainer venueContainer;
	private long conferenceId;
	private List<Overlay> venueOverlays;
	private VenueItemizedOverlay venueItemizedOverlay;


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.venuemap);

		conferenceId = getIntent().getIntExtra("id", -1);

		if(conferenceId == -1){
			Toast.makeText(this, "Error. No such conference id (" + conferenceId + ")", Toast.LENGTH_LONG).show();
			finish();
		}

		venueItemizedOverlay = new VenueItemizedOverlay(getResources().getDrawable(R.drawable.pin),this);
		venueMap = (MapView) findViewById(R.id.mapview);
		venueMap.setBuiltInZoomControls(true);
		try {
			venueContainer = new VenueContainer(Conference.insertId(getString(R.string.getVenus), conferenceId));
		} catch (ConnectException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		venueOverlays = venueMap.getOverlays();

		for(Venue v : venueContainer){
			OverlayItem item = new OverlayItem(v.getGeoPoint(), v.getName(), v.getAddress());
			venueItemizedOverlay.addOverlay(item);
			venueMap.getController().setCenter(v.getGeoPoint());
		}

		venueOverlays.add(venueItemizedOverlay);

	}

	public void onResume(){
		super.onResume();

		Log.i(TAG, "onResume");


		double lon,lat;

		Intent intent = getIntent();

		lon = intent.getDoubleExtra("lon", 0);
		lat = intent.getDoubleExtra("lat", 0);


		GeoPoint point = new GeoPoint((int)(lat * 1000000), (int)(lon *  1000000));
		centerMap(point);
		venueMap.getController().setZoom(intent.getIntExtra("zoom", 0));
		
	}

	private void centerMap(GeoPoint point){
		venueMap.getController().setCenter(point);
	}




	@Override
	protected boolean isRouteDisplayed() {

		return false;
	}



}
